package top.buaarobot.webserver.web.rest;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import top.buaarobot.webserver.domain.Resume;
import top.buaarobot.webserver.repository.ResumeRepository;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ResumeResource {

    private final ResumeRepository resumeRepository;

    public ResumeResource(ResumeRepository resumeRepository) {
        this.resumeRepository = resumeRepository;
    }

    @GetMapping(value = "/resume")
    public ResponseEntity<Iterable<Resume>> getAllResumes(){
        Iterable<Resume> resumes = resumeRepository.findAll();
        return ResponseEntity.ok().body(resumes);
    }

    @PostMapping(value = "/resume")
    public ResponseEntity<String> submit(@RequestBody JSONObject json){
        System.out.println(json);
        Resume resume = new Resume();
        resume.setName(json.getString("name"));
        resume.setSex(json.getString("sex"));
        resume.setGrade(json.getString("grade"));
        resume.setAcademy(json.getString("academy"));
        resume.setPhone(json.getString("phone"));
        resume.setEmail(json.getString("email"));
        resume.setChoice(json.getString("choice"));
        resume.setExperience(json.getString("experience"));
        resume.setExpert(json.getString("expert"));
        resume.setWhy(json.getString("why"));
        resume.setSuggestion(json.getString("suggestion"));
        resume.setDate(json.getString("date"));
        resumeRepository.save(resume);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/resume/delete/{id}")
    public ResponseEntity postRecord(@PathVariable Integer id){

        resumeRepository.deleteById(id);
        return ResponseEntity.ok().build();
    }
}
